package com.jojo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.constant.StatusEnum;
import com.jojo.domain.MemberAddr;
import com.jojo.domain.OrderItem;
import com.jojo.exception.BusinessException;
import com.jojo.feign.OrderMemberFeign;
import com.jojo.mapper.OrderItemMapper;
import com.jojo.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jojo.mapper.OrderMapper;
import com.jojo.domain.Order;
import com.jojo.service.OrderService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Resource
    private OrderMemberFeign orderMemberFeign;


    @Override
    public Page<Order> queryOrderPage(Page<Order> page, String orderNumber, Integer status, Date startTime, Date endTime) {
        page = orderMapper.selectPage(page,new LambdaQueryWrapper<Order>()
                .eq(ObjectUtil.isNotNull(status),Order::getStatus,status)
                .between(ObjectUtil.isAllNotEmpty(startTime,endTime),Order::getCreateTime,startTime,endTime)
                .eq(StringUtils.hasText(orderNumber),Order::getOrderNumber,orderNumber)
                .orderByDesc(Order::getCreateTime)
        );
        //从订单分页对象中获取订单条目
        List<Order> records = page.getRecords();
        if (CollectionUtil.isEmpty(records) || records.size()==0) {
            return page;
        }
        //从订单记录集合中获取订单编号结合
        List<String> orderNumberList = records.stream().map(Order::getOrderNumber).collect(Collectors.toList());
        //根据订单编号查询所有订单商品条目对象集合
        List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
                .in(OrderItem::getOrderNumber, orderNumberList)
        );
        //循环遍历订单记录集合
        records.forEach(order -> {
            //从订单商品条目对象集合中过滤出与当前订单记录的编号一致的商品条目对象集合
            List<OrderItem> itemList = orderItemList.stream()
                    .filter(orderItem -> orderItem.getOrderNumber().equals(order.getOrderNumber()))
                    .collect(Collectors.toList());
            order.setOrderItems(itemList);
        });
        return page;
    }

    @Override
    public Order queryOrderDetailByOrderNumber(Long orderNumber) {
        // 根据订单编号查询订单信息
        Order order = orderMapper.selectOne(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderNumber)
        );
        // 根据订单编号查询订单商品条目对象集合
        List<OrderItem> itemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
                .eq(OrderItem::getOrderNumber, orderNumber)
        );
        order.setOrderItems(itemList);
        //从订单记录中获取订单收货地址标识
        Long addrOrderId = order.getAddrOrderId();
        // 【远程调用 -- 订单调用会员】  根据收货地址标识查询地址详情
        Result<MemberAddr> result = orderMemberFeign.getMemberAddrById(addrOrderId);
        if (result.getCode().equals(StatusEnum.OPERATION_FAIL.getCode())){
            throw new BusinessException("远程接口调用失败: 根据收货地址标识査询收货地址信息");
        }
        order.setUserAddrOrder(result.getData());
        //根据会员openid查询会员名称
        Result<String> result1 = orderMemberFeign.getNickNameByOpenId(order.getOpenId());
        if (result1.getCode().equals(StatusEnum.OPERATION_FAIL.getCode())){
            throw new BusinessException("远程接口调用失败: 恨据会员openId查询会员昵称");
        }
        order.setNickName(result1.getData());
        return order;
    }
}
